package com.shaohai.live.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.shaohai.live.R;
import com.shaohai.live.utils.ChatMessage;

import java.util.List;

public class ChatMessageAdapter extends BaseAdapter {
    List<ChatMessage> dataArray;
    Context context;

    public ChatMessageAdapter(Context context, List<ChatMessage> courses) {
        this.dataArray = courses;
        this.context = context;
    }

    @Override
    public int getCount() {
        return dataArray.size();
    }

    @Override
    public Object getItem(int i) {
        return dataArray.get(i);
    }

    @Override
    public long getItemId(int i) {
        return dataArray.size();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ItemContainerView itemContainerView;
        if (convertView == null) {
            itemContainerView = new ItemContainerView();
            convertView = LayoutInflater.from(context).inflate(R.layout.chat_message_item, null);
            itemContainerView.tv_username = convertView.findViewById(R.id.tv_username);
            itemContainerView.tv_title = convertView.findViewById(R.id.tv_message);
            convertView.setTag(itemContainerView);
        } else {
            itemContainerView = (ItemContainerView) convertView.getTag();
        }
        ChatMessage data = dataArray.get(position);
        itemContainerView.tv_title.setText(data.message);
        itemContainerView.tv_username.setText(data.username);
        return convertView;
    }


    class ItemContainerView {
        TextView tv_title, tv_username;
    }
}
